package com.liuhangs.learning.crowd.webui.mapper;

import com.liuhangs.learning.crowd.webui.bean.auth.Permission;
import com.liuhangs.learning.crowd.webui.bean.auth.Role;
import com.liuhangs.learning.crowd.webui.bean.auth.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**管理员用户权限查询
 * @author 11757
 * @DATE 2020/6/8
 */
@Repository
public interface AdminUserMapper {

    /**
     * 根据账号名查询用户信息,不包含角色和权限信息
     * account
     * @return
     */
    @Select({"select id,account,password,user_name,email from t_user where account = #{account}"})
    @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="account",column="account"),
            @Result(property="password",column="password"),
            @Result(property="userName",column="user_name"),
            @Result(property="email",column="email"),
            })
    public User querySimpleUserInfoByAcc(String account);

    /**
     * 根据账号名查询用户信息,只包含用户和角色信息
     * account
     * @return
     */
    @Select({"select id,account,password,user_name,email from t_user where account = #{account}"})
    @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="account",column="account"),
            @Result(property="password",column="password"),
            @Result(property="userName",column="user_name"),
            @Result(property="email",column="email"),
            @Result(property="roles",column="id", many=@Many(select = "com.liuhangs.learning.crowd.webui.mapper.AdminUserMapper.querySimpleRoleInfoByUserId"))
    })
    public User queryUserRoleInfoByAcc(String account);

    /**
     * 根据账号名查询用户信息，包括权限和角色信息
     * account
     * @return
     */
    @Select({"select id,account,password,user_name,email from t_user where account = #{account}"})
    @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="account",column="account"),
            @Result(property="password",column="password"),
            @Result(property="userName",column="user_name"),
            @Result(property="email",column="email"),
            @Result(property="roles",column="id", many=@Many(select = "com.liuhangs.learning.crowd.webui.mapper.AdminUserMapper.queryRoleInfoByUserId"))
    })
    public User queryUserInfoByAcc(String account);

    /**
     * 根据账号名查询简单角色信息，不包含权限信息
     * account
     * @return
     */
    @Select({"select t2.id,t2.role_name,t2.description,t2.status from t_user_role t1 inner join t_role t2 on t1.role_id = t2.id and t1.user_id = #{userId}"})
    @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="roleName",column="role_name"),
            @Result(property="description",column="description"),
            @Result(property="status",column="status"),
            })
    public List<Role> querySimpleRoleInfoByUserId(int userId);

    /**
     * 根据账号名查询角色信息
     * account
     * @return
     */
    @Select({"select t2.id,t2.role_name,t2.description,t2.status from t_user_role t1 inner join t_role t2 on t1.role_id = t2.id and t1.user_id = #{userId}"})
    @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="roleName",column="role_name"),
            @Result(property="description",column="description"),
            @Result(property="status",column="status"),
            @Result(property="permissions",column="id", many=@Many(select = "com.liuhangs.learning.crowd.webui.mapper.AdminUserMapper.queryPermissionInfoByRoleId"))
    })
    public List<Role> queryRoleInfoByUserId(int userId);

    /**
     * 根据账号名查询用户信息
     * account
     * @return
     */
    @Select({"select t2.id,t2.code,t2.description,t2.url from t_role_permission t1 inner join t_permission t2 on t1.permission_id = t2.id and t1.role_id = #{role_id}"})
    @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="code",column="code"),
            @Result(property="description",column="description"),
            @Result(property="url",column="url")
    })
    public List<Permission> queryPermissionInfoByRoleId(int roleId);
}
